package com.way.view.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.google.gson.Gson;
import com.way.bean.GroupBean;
import com.way.dialer.R;

public class MenuListAdapter extends BaseAdapter {

	private Activity mContext;
	private int mItemCount;
	private LayoutInflater mLayoutInflater;
	private boolean mIsPressed[];
	private int mPressedId;
	private List<GroupBean> mGroupBeanLists;

	public MenuListAdapter(Activity context, List<GroupBean> list) {
		this.mGroupBeanLists = list;
		this.mContext = context;
		this.init();
	}

	private void init() {
		this.mItemCount = this.mGroupBeanLists.size();
		this.mIsPressed = new boolean[this.mItemCount];
		for (int i = 0; i < this.mItemCount; i++) {
			this.mIsPressed[i] = false;
		}
		this.mIsPressed[this.mPressedId] = true;
		this.mLayoutInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return this.mGroupBeanLists.size();
	}

	@Override
	public Object getItem(int position) {
		return this.mGroupBeanLists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = this.mLayoutInflater.inflate(R.layout.menu_list_item,
					null);
			viewHolder.menuText = (TextView) convertView
					.findViewById(R.id.menuText);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.menuText.setText(mGroupBeanLists.get(position).getName());

		if (mIsPressed[position])
			convertView.setBackgroundResource(R.drawable.menu_item_bg_sel);
		else
			convertView.setBackgroundResource(R.drawable.menu_item_bg_sel_off);

		addViewListener(convertView, mGroupBeanLists.get(position), position);
		return convertView;
	}

	public final class ViewHolder {
		public TextView menuText;
	}

	private void addViewListener(View view, final GroupBean gb,
			final int position) {

		view.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				changeState(position);
				// gotoActivity(po);
				Gson gson = new Gson();
				String bean = gson.toJson(gb);
				Intent it1 = new Intent();
				it1.setAction("SET_DEFAULT_SIG");
				it1.putExtra("groupbean", bean);
				mContext.sendBroadcast(it1);

				notifyDataSetInvalidated();
				new Handler().post(new Runnable() {
					public void run() {

					}
				});
			}
		});
	}

	// private void gotoActivity(int position){
	// Intent intent = new Intent();
	// switch(position){
	// case 0:
	//
	// if(this.pressedId == 0){
	// MainActivity activity = (MainActivity)context;
	// activity.getScrollView().clickMenuBtn(context);
	// }
	// else{
	// intent.setClass(context, MainActivity.class);
	// context.startActivity(intent);
	// context.overridePendingTransition(R.anim.push_in,R.anim.push_out);
	// context.finish();
	// }
	// System.out.println("case11111111");
	// break;
	// /*----------------------------------------------------*/
	// case 1:
	// // if(this.pressedId == 1){
	// // CAROLPageActivity activity = (CAROLPageActivity)context;
	// // activity.getScrollView().clickMenuBtn(context);
	// // }
	// // else{
	// // intent.setClass(context, CAROLPageActivity.class);
	// // context.startActivity(intent);
	// // context.overridePendingTransition(R.anim.push_in,R.anim.push_out);
	// // context.finish();
	// // }
	// System.out.println("case22222222");
	// break;
	// /*----------------------------------------------------*/
	// default:
	// System.out.println("case33333333");
	// // intent.setClass(context, WebPageActivity.class);
	// // context.startActivity(intent);
	// }
	// }

	private void changeState(int position) {

		for (int i = 0; i < this.mItemCount; i++) {
			mIsPressed[i] = false;
		}
		mIsPressed[position] = true;
	}

}
